﻿
using Maticsoft.BLL;
using Maticsoft.DBUtility;
using Maticsoft.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Utils;
using Wuqi.Webdiyer;

public partial class WebPage_Dingdan_WeifuDingdan : OrderBBasePage
{

    protected string beginTime = "";
    public Maticsoft.BLL.Card cardbll = new Maticsoft.BLL.Card();
    public Maticsoft.Model.Card cardmodel = new Maticsoft.Model.Card();
    public Maticsoft.BLL.Coupon coupbll = new Maticsoft.BLL.Coupon();
    public Maticsoft.Model.Coupon coupmodel = new Maticsoft.Model.Coupon();
    public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
    public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();
    private Maticsoft.Model.Flow flow = new Maticsoft.Model.Flow();
    private Maticsoft.BLL.Flow flowbll = new Maticsoft.BLL.Flow();
    protected string lasttime = "";

    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
    public int pagenum = 0;
    protected string param = string.Empty;
    protected string strTeamid = "";
    public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
    public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        if (base.Request["Type"] != "")
        {
            this.Session["Type"] = base.Request["Type"];
        }
        this.getWeifu();
    }

    private void deleteOrder(int teamid, int orderid)
    {
        Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
        if (Helper.GetInt(teamid, 0) == 0)
        {
            this.detaillist = this.detailbll.Searchorderdetail(orderid);
            foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
            {
                this.cardmodel = this.cardbll.GetTeamid(Convert.ToInt32(orderdetail.Teamid), Convert.ToInt32(orderdetail.Order_id));
                if (this.cardmodel != null)
                {
                    this.cardmodel.Team_id = 0;
                    this.cardmodel.Partner_id = 0;
                    this.cardmodel.consume = "N";
                    this.cardmodel.Order_id = 0;
                    this.cardbll.Update(this.cardmodel);
                }
            }
        }
        else
        {
            this.cardmodel = this.cardbll.GetTeamid(teamid, orderid);
            if (this.cardmodel != null)
            {
                this.cardmodel.Team_id = 0;
                this.cardmodel.Partner_id = 0;
                this.cardmodel.Order_id = 0;
                this.cardmodel.consume = "N";
                this.cardbll.Update(this.cardmodel);
            }
            order.Delete(orderid);
        }
    }

    public string getTime()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<option value=''></option>");
        for (int i = 0; i < 0x18; i++)
        {
            builder.Append(string.Concat(new object[] { "<option value='", i, "'>", i, "</option>" }));
        }
        return builder.ToString();
    }

    private void getWeifu()
    {
        this.Session["sql"] = this.SqlWhere;
        StringBuilder builder = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        builder.Append("<tr >");
        builder.Append("<th width='40'>ID</th>");
        builder.Append("<th class='xiangmu'>项目</th>");
        builder.Append("<th width='70'>用户</th>");
        builder.Append("<th width='30' nowrap>数量</th>");
        builder.Append("<th width='50' nowrap>总款</th>");
        builder.Append("<th width='50' nowrap>余付</th>");
        builder.Append("<th width='50' nowrap>支付</th>");
        builder.Append("<th width='30' nowrap>操作</th>");
        builder.Append("</tr>");
        Maticsoft.Model.Team model = new Maticsoft.Model.Team();
        Maticsoft.BLL.Team team2 = new Maticsoft.BLL.Team();
        Maticsoft.Model.UserInfo info = new Maticsoft.Model.UserInfo();
        Maticsoft.BLL.UserInfo info2 = new Maticsoft.BLL.UserInfo();
        Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
        Maticsoft.Model.Order order2 = new Maticsoft.Model.Order();
        string tablename = "";
        Pagers pagers = null;
        if (this.Session["Type"] != null)
        {
            if (Convert.ToBoolean(this.Session["Type"].ToString()))
            {
                tablename = "[order] left join [orderdetail] on([order].id=[orderdetail].order_id)";
                pagers = DbHelperSQL.SelectByPager("[order].*", this.SqlWhere, "[order].create_time desc", this.pagenum, this.AspNetPager1.PageSize, tablename, "", "");
            }
            else
            {
                tablename = "[order] left join [user] on [order].user_id=[user].id";
                pagers = DbHelperSQL.SelectByPager("[order].*", this.SqlWhere, "[order].create_time desc", this.pagenum, this.AspNetPager1.PageSize, tablename, "", "");
            }
            this.Session.Add("strTable", tablename);
        }
        else if (this.Session["strTable"] != null)
        {
            pagers = DbHelperSQL.SelectByPager("[order].*", this.SqlWhere, "[order].create_time desc", this.pagenum, this.AspNetPager1.PageSize, this.Session["strTable"].ToString(), "", "");
        }
        else
        {
            pagers = DbHelperSQL.SelectByPager("*", this.SqlWhere, "create_time desc", this.pagenum, this.AspNetPager1.PageSize, "[order]", "", "");
        }
        if (pagers.MyData.Rows.Count > 0)
        {
            int num = 0;
            for (int i = 0; i < pagers.MyData.Rows.Count; i++)
            {
                DataRow row = pagers.MyData.Rows[i];
                if ((num % 2) != 0)
                {
                    builder2.Append("<tr>");
                }
                else
                {
                    builder2.Append("<tr class='alt'>");
                }
                builder2.Append("<td>" + row["Id"].ToString() + "</td>");
                this.ordermodel = this.orderbll.GetModel(int.Parse(row["Id"].ToString()));
                if (this.ordermodel != null)
                {
                    model = team2.GetModel(this.ordermodel.Team_id);
                    if (model == null)
                    {
                        this.detaillist = this.detailbll.Searchorderdetail(Convert.ToInt32(row["Id"].ToString()));
                        builder2.Append("<td>&nbsp;");
                        int num3 = 0;
                        foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                        {
                            num3++;
                            builder2.Append(string.Concat(new object[] { "(<a class='deal-title' href='../team.aspx?id=", orderdetail.Teamid, "' target='_blank'>", num3, ":" }));
                            builder2.Append((this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)) == null) ? "" : string.Concat(new object[] { this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)).Title, "<font style='color:red'>", Maticsoft.BLL.Order.Getbulletin(orderdetail.Result), "</font><font style='color:red'>(", orderdetail.Num, "件)</font>" }));
                            builder2.Append("</a>)<br>");
                        }
                        builder2.Append("</td>");
                    }
                    else
                    {
                        builder2.Append(string.Concat(new object[] { "<td>", this.ordermodel.Team_id, "&nbsp;(<a class='deal-title' href='/team.aspx?id=", this.ordermodel.Team_id, "' target='_blank'>", model.Title, "<font style='color:red'>", Maticsoft.BLL.Order.Getbulletin(this.ordermodel.Result), "</font></a>)</td>" }));
                    }
                    info = info2.GetModel(this.ordermodel.User_id);
                    if (info != null)
                    {
                        builder2.Append(string.Concat(new object[] { "<td><a href='/ajax/manage.aspx?action=userview&id=", this.ordermodel.User_id, "' class='ajaxlink'>", info.Email, "<br/>", info.Username, "</a>&nbsp;&raquo;&nbsp;<a href='/ajax/misc.aspx?action=sms&v=", info.Mobile, "' class='ajaxlink'>短信</a></td>" }));
                    }
                    else
                    {
                        builder2.Append("<td>&nbsp;</td>");
                    }
                    order2 = order.GetModel(int.Parse(row["Id"].ToString()));
                    builder2.Append("<td>" + this.ordermodel.Quantity + "</td>");
                    string currency = "\x00a5";
                    if (base.ASSystem != null)
                    {
                        currency = base.ASSystem.Currency;
                    }
                    else
                    {
                        currency = "&nbsp;";
                    }
                    builder2.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", this.ordermodel.Origin, "</td>" }));
                    builder2.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", this.ordermodel.Credit, "</td>" }));
                    builder2.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", this.ordermodel.Money, "</td>" }));
                    builder2.Append(string.Concat(new object[] { 
                        "<td class='op' nowrap><a href='/ajax/manage.aspx?action=orderview&orderview=", row["Id"], "&pagenum=", this.pagenum, "&y=", this.param, "' class='ajaxlink'>详情</a>|<a ask='确定本单为现金付款?' href='Dingdan_WeifuDingdan.aspx?id=", row["Id"], "'>现金</a> |<a ask='确定删除本单吗?' href='Dingdan_WeifuDingdan.aspx?delete=", row["Id"], "&teamid=", this.ordermodel.Team_id, "&pagenum=", this.pagenum, "&type=", this.Session["Type"], 
                        "' class=''>删除</a></td>"
                     }));
                    builder2.Append("</tr>");
                    num++;
                }
            }
        }
        this.AspNetPager1.RecordCount = pagers.TotalRecords;
        this.Literal1.Text = builder.ToString();
        this.Literal2.Text = builder2.ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.Page.Header.Controls.Add(new LiteralControl("<script src='" + base.WebRoot + "upfile/js/datePicker/WdatePicker.js'></script>\r\n"));
        this.HeaderControl1.Select = "order";
        if (base.Request["id"] != null)
        {
            base.SetSuccess("修改成功！");
            this.UpdadteFlow(Convert.ToInt32(base.Request["id"].ToString()));
            this.Updateorder(Convert.ToInt32(base.Request["id"].ToString()));
        }
        this.param = base.Request["y"];
        if (base.Request.QueryString["delete"] != null)
        {
            this.deleteOrder(Convert.ToInt32(base.Request["teamid"]), int.Parse(base.Request.QueryString["delete"].ToString()));
            base.SetSuccess("删除成功！");
        }
        if (!this.Page.IsPostBack)
        {
            this.Session.Remove("Type");
            if (base.Request["pagenum"] != null)
            {
                if (Utility.IsNum(base.Request["pagenum"].ToString()))
                {
                    this.pagenum = Convert.ToInt32(base.Request["pagenum"]);
                }
                else
                {
                    this.SetError("友情提示：参数非法");
                }
            }
            if (base.Request.QueryString["pagenum"] != null)
            {
                this.SqlWhere = this.Session["sql"].ToString();
                this.pagenum = Convert.ToInt32(base.Request.QueryString["pagenum"]);
                if (base.Request.QueryString["type"] != "")
                {
                    this.Session["Type"] = base.Request.QueryString["type"];
                }
            }
            else
            {
                this.setSqlWhere();
            }
        }
        if (base.Request["btnselect"] == "筛选")
        {
            this.Session.Remove("Type");
            this.setSqlWhere();
        }
        this.getWeifu();
    }

    public void setSqlWhere()
    {
        DateTime time;
        DateTime time2;
        string str = " state='unpay' ";
        if ((((this.ddlstate.SelectedValue != null) && (this.txtcontent.Text != null)) && (this.ddlstate.SelectedValue.ToString() != "")) && (this.txtcontent.Text.ToString() != ""))
        {
            switch (this.ddlstate.SelectedValue.ToString())
            {
                case "1":
                    this.Session.Add("Type", false);
                    str = str + " and Username like '" + this.txtcontent.Text.ToString().Trim() + "%' ";
                    break;

                case "2":
                    this.Session.Add("Type", false);
                    str = str + " and Email like '" + this.txtcontent.Text.ToString().Trim() + "%' ";
                    break;

                case "3":
                    this.Session.Add("Type", false);
                    try
                    {
                        str = str + " and [order].Id=" + Convert.ToInt32(this.txtcontent.Text.ToString());
                    }
                    catch (Exception)
                    {
                        this.SetError("订单编号输入格式错误");
                    }
                    break;

                case "4":
                    try
                    {
                        this.Session.Add("Type", true);
                        str = string.Concat(new object[] { str, " and ((teamid=", Convert.ToInt32(this.txtcontent.Text.ToString()), ") or (team_id=", Convert.ToInt32(this.txtcontent.Text.ToString()), ")) " });
                    }
                    catch (Exception)
                    {
                        this.SetError("项目编号输入格式错误");
                    }
                    break;

                case "5":
                    try
                    {
                        this.Session.Add("Type", false);
                        str = str + " and Pay_id like '" + this.txtcontent.Text.ToString().Substring(0, this.txtcontent.Text.ToString().LastIndexOf("as")) + "%' ";
                    }
                    catch (Exception)
                    {
                        this.SetError("交易单号输入格式错误");
                        base.Response.Redirect(base.Request.Url.ToString());
                        base.Response.End();
                        return;
                    }
                    break;

                case "6":
                    this.Session.Add("Type", false);
                    str = str + " and Express_no='" + this.txtcontent.Text.ToString().Trim() + "' ";
                    break;

                case "7":
                    this.Session.Add("Type", false);
                    str = str + " and [order].Mobile like '" + this.txtcontent.Text.ToString().Trim() + "%' ";
                    break;
            }
        }
        if ((this.firstTime.Text != null) && (this.firstTime.Text != ""))
        {
            try
            {
                time = Convert.ToDateTime(this.firstTime.Text);
            }
            catch (Exception)
            {
                this.SetError("您输入的时间格式不正确");
            }
        }
        if ((this.endTime.Text != null) && (this.endTime.Text != ""))
        {
            try
            {
                time2 = Convert.ToDateTime(this.endTime.Text);
            }
            catch (Exception)
            {
                this.SetError("您输入的时间格式不正确");
            }
        }
        if ((((this.firstTime.Text != null) && (this.endTime.Text != null)) && (this.firstTime.Text != "")) && (this.endTime.Text != ""))
        {
            try
            {
                time = Convert.ToDateTime(this.firstTime.Text);
                time2 = Convert.ToDateTime(this.endTime.Text);
            }
            catch (Exception)
            {
                this.SetError("您输入的时间格式不正确");
            }
            if ((base.Request.Form["time"] != null) && (base.Request.Form["time"].ToString() != ""))
            {
                str = str + " and (create_time BETWEEN '" + this.firstTime.Text + " " + base.Request.Form["time"].ToString() + ":00:00' and '" + this.endTime.Text + " " + base.Request.Form["time"].ToString() + ":59:59'  ) ";
            }
            else
            {
                str = str + " and (create_time BETWEEN '" + this.firstTime.Text + " 00:00:00' and '" + this.endTime.Text + " 23:59:59'  ) ";
            }
        }
        this.SqlWhere = str;
    }

    public void UpdadteFlow(int id)
    {
        this.ordermodel = this.orderbll.GetModel(id);
        if (HttpContext.Current.User.Identity.Name != null)
        {
            this.flow.User_id = this.ordermodel.User_id;
            this.flow.Admin_id = this.userbll.Getuserid(HttpContext.Current.User.Identity.Name.ToString());
            this.flow.Detail_id = id.ToString();
            this.flow.Direction = "expense";
            this.flow.Money = this.ordermodel.Origin;
            this.flow.Action = "cash";
            this.flow.Create_time = DateTime.Now;
            this.flowbll.Add(this.flow);
        }
        else
        {
            this.SetError("支付失败,请重新登录");
        }
    }

    public void Updateorder(int id)
    {
        this.ordermodel = this.orderbll.GetModel(id);
        this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
        this.usermodel = this.userbll.GetModel(this.ordermodel.User_id);
        if (this.ordermodel != null)
        {
            int num2;
            int num3;
            int num4;
            if (this.teammodel != null)
            {
                if (this.teammodel.Delivery == "coupon")
                {
                    Maticsoft.BLL.Order.UpdateNumber(this.ordermodel.Team_id, this.ordermodel.Quantity);
                    this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
                    if (Maticsoft.BLL.Team.istuan(Convert.ToInt32(this.teammodel.Now_number.ToString()), Convert.ToInt32(this.teammodel.Min_number)))
                    {
                        for (int i = 0; i < this.ordermodel.Quantity; i++)
                        {
                            this.coupmodel.Team_id = this.ordermodel.Team_id;
                            this.coupmodel.Order_id = id;
                            this.coupmodel.Type = "consume";
                            this.coupmodel.Credit = this.teammodel.Credit;
                            this.coupmodel.Consume = "N";
                            this.coupmodel.User_id = this.ordermodel.User_id;
                            this.coupmodel.Create_time = DateTime.Now;
                            this.coupmodel.Expire_time = this.teammodel.Expire_time;
                            this.coupmodel.Partner_id = this.teammodel.Partner_id;
                            this.coupmodel.Id = Helper.GetRandomString(12);
                            this.coupmodel.Secret = Utility.GetCouponpass();
                            this.coupbll.Add(this.coupmodel);
                            num2 = this.teammodel.Min_number;
                            num3 = this.teammodel.Max_number;
                            num4 = this.teammodel.Now_number;
                            if ((num4 >= num3) && !(this.teammodel.Close_time.HasValue && !(this.teammodel.Close_time.ToString() == "")))
                            {
                                this.teambll.updateValue(this.ordermodel.Team_id.ToString(), "Close_time", DateTime.Now.ToString());
                            }
                            if ((num4 >= num2) && !(this.teammodel.Reach_time.HasValue && !(this.teammodel.Reach_time.ToString() == "")))
                            {
                                this.teambll.updateValue(this.ordermodel.Team_id.ToString(), "Reach_time", DateTime.Now.ToString());
                            }
                            this.sysmodel = this.sysbll.GetModelByCache(1);
                            string sitename = "";
                            if (this.sysmodel != null)
                            {
                                sitename = this.sysmodel.sitename;
                            }
                            List<string> mobiles = new List<string>();
                            this.usermodel = this.userbll.GetModel(this.ordermodel.User_id);
                            if (this.usermodel != null)
                            {
                                mobiles.Add(this.usermodel.Mobile);
                                string content = sitename + "项目：" + this.teammodel.Product + "，编号：" + this.coupmodel.Id + "，密码：" + this.coupmodel.Secret + "，有效期至：" + this.coupmodel.Expire_time.ToString("yyyy年MM月dd日");
                                Maticsoft.BLL.Order.SendSMS(mobiles, content);
                            }
                        }
                    }
                }
            }
            else
            {
                this.detaillist = this.detailbll.Searchorderdetail(this.ordermodel.Id);
                foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                {
                    Maticsoft.BLL.Order.UpdateNumber(Convert.ToInt32(orderdetail.Teamid), Convert.ToInt32(orderdetail.Num));
                    this.teammodel = this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid));
                    num2 = this.teammodel.Min_number;
                    num3 = this.teammodel.Max_number;
                    num4 = this.teammodel.Now_number;
                    if ((num4 >= num3) && !(this.teammodel.Close_time.HasValue && !(this.teammodel.Close_time.ToString() == "")))
                    {
                        this.teambll.updateValue(orderdetail.Teamid.ToString(), "Close_time", DateTime.Now.ToString());
                    }
                    if ((num4 >= num2) && !(this.teammodel.Reach_time.HasValue && !(this.teammodel.Reach_time.ToString() == "")))
                    {
                        this.teambll.updateValue(orderdetail.Teamid.ToString(), "Reach_time", DateTime.Now.ToString());
                    }
                }
            }
            this.ordermodel.State = "pay";
            this.ordermodel.Service = "cash";
            this.ordermodel.Admin_id = base.AsUser.Id;
            this.ordermodel.Pay_time = new DateTime?(DateTime.Now);
            this.orderbll.Update(this.ordermodel);
        }
    }

    public string SqlWhere
    {
        get
        {
            if (this.ViewState["sqlWhere"] != null)
            {
                return this.ViewState["sqlWhere"].ToString();
            }
            return " State='unpay' ";
        }
        set
        {
            this.ViewState["sqlWhere"] = value;
        }
    }
}

